package com.xuanfu_button;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import net.xxhong.rosclient.R;

import java.util.ArrayList;
import java.util.List;

import org_rosjava.ros.android.android_tutorial_map_viewer.My_map;
import xia.mylog.L;

/**
 * 创建自定义的dialog，主要学习其实现原理
 * Created by chengguo on 2016/3/22.
 */
public class SelfDialog_with_type extends Dialog {

    private Button yes;//确定按钮  
    private Button no;//取消按钮  
    private TextView titleTv;//消息标题文本  
    private EditText message_input;//输入
    private String titleStr;//从外界设置的title文本  
    private String messageStr;//从外界设置的消息文本  
    //确定文本和取消文本的显示内容  
    private String yesStr, noStr;
    boolean is_no_type_point=false;
    private onNoOnclickListener noOnclickListener;//取消按钮被点击了的监听器  
    private onYesOnclickListener yesOnclickListener;//确定按钮被点击了的监听器  
    private Button mContractBtn;
    private Context mContext;
    //private RadioGroup radioGroup;
    //private RadioButton radio_kuaidi, radio_xunluo;
    private String get_chat_people_name="";
    private String message_input_type="";
    private String message_input_type_last="";
    /**
     * 设置取消按钮的显示内容和监听
     *
     * @param str
     * @param onNoOnclickListener
     */
    public void setNoOnclickListener(String str, onNoOnclickListener onNoOnclickListener) {
        if (str != null) {
            noStr = str;
        }
        this.noOnclickListener = onNoOnclickListener;
    }

    /**
     * 设置确定按钮的显示内容和监听
     *
     * @param str
     * @param onYesOnclickListener
     */
    public void setYesOnclickListener(String str, onYesOnclickListener onYesOnclickListener) {
        if (str != null) {
            yesStr = str;
        }
        this.yesOnclickListener = onYesOnclickListener;
    }

    public SelfDialog_with_type(Context context) {
        super(context, R.style.MyDialog);
        mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.showinputdialog_with_type);
        //按空白处不能取消动画  
        setCanceledOnTouchOutside(false);

        //初始化界面控件  
        initView();
        //初始化界面数据  
        initData();
        //初始化界面控件的事件  
        initEvent();

    }

    /**
     * 初始化界面的确定和取消监听器
     */
    private void initEvent() {
        //设置确定按钮被点击后，向外界提供监听  
        yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (yesOnclickListener != null) {
                    yesOnclickListener.onYesClick();
                }
            }
        });
        //设置取消按钮被点击后，向外界提供监听  
        no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (noOnclickListener != null) {
                    noOnclickListener.onNoClick();
                }
            }
        });
        /*
        mContractBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final NameListDialog dialog = new NameListDialog(mContext);
                dialog.show();
                dialog.putData(MyApp.namelist);
                dialog.setOnDialogItemClickListener(new NameListDialog.OnDialogItemClickListener() {
                    @Override
                    public void onDialogItemClick(String name) {
                        message_input.setText(name);
                        message_input.setSelection(name.length());
                        dialog.dismiss();
                    }
                });
            }
        });
        */

        mContractBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                get_chat_people_name="";
                if (My_map.getinstance().mark_coordinatejsondata_last == null) {
                    //ST.showToast(mContext, "底盘通讯异常，请稍后");
                    Toast.makeText(My_map.getinstance(),"底盘通讯异常，请稍后",Toast.LENGTH_LONG).show();
                    return;
                }
                if (My_map.getinstance().namelist==null) {
                    Toast.makeText(My_map.getinstance(),"获取联系人异常，请稍后",Toast.LENGTH_LONG).show();
                    return;
                }
                List<String> namelist_tmp= new ArrayList<>();
                for (int i = 0; i < My_map.getinstance().namelist.size(); i++) {
                    namelist_tmp.add(My_map.getinstance().namelist.get(i));
                }


                My_map.getinstance().markNamelist.clear();
                for (int i = 0; i < My_map.getinstance().mark_coordinatejsondata_last.mark_coordinate.size(); i++) {
                    if(My_map.getinstance().mark_coordinatejsondata_last.mark_coordinate.get(i).name.contains(My_map.point_type_flag))
                        My_map.getinstance().markNamelist.add(My_map.getinstance().mark_coordinatejsondata_last.mark_coordinate.get(i).name.substring(0,My_map.getinstance().mark_coordinatejsondata_last.mark_coordinate.get(i).name.indexOf(My_map.point_type_flag)));
                    else
                        My_map.getinstance().markNamelist.add(My_map.getinstance().mark_coordinatejsondata_last.mark_coordinate.get(i).name);
                }
                L.e("markNamelist="+My_map.getinstance().markNamelist.toString());
                for (int i = 0; i < My_map.getinstance().markNamelist.size(); i++) {
                    if (namelist_tmp.contains(My_map.getinstance().markNamelist.get(i))) {
                        namelist_tmp.remove(My_map.getinstance().markNamelist.get(i));
                    }
                }
                if (namelist_tmp.size() < 1) {
                    //ST.showToast(mContext, "成员已经全部在图上了");
                    Toast.makeText(My_map.getinstance(),"成员已经全部在图上了",Toast.LENGTH_LONG).show();
                } else {
                    final NameListDialog dialog = new NameListDialog(mContext);
                    dialog.show();
                    dialog.putData(namelist_tmp);
                    dialog.setOnDialogItemClickListener(new NameListDialog.OnDialogItemClickListener() {
                        @Override
                        public void onDialogItemClick(String name) {
                            get_chat_people_name=name;
                            message_input.setText(name);
                            message_input.setSelection(name.length());
                            dialog.dismiss();
                        }
                    });
                }
            }
        });
    }

    /**
     * 初始化界面控件的显示数据
     */
    private void initData() {
        //如果用户自定了title和message  
        if (titleStr != null) {
            titleTv.setText(titleStr);
        }
        if (messageStr != null) {
            message_input.setText(messageStr);
        }
        //如果设置按钮的文字  
        if (yesStr != null) {
            yes.setText(yesStr);
        }
        if (noStr != null) {
            no.setText(noStr);
        }
        get_chat_people_name="";
    }

    /**
     * 初始化界面控件
     */
    private void initView() {
        yes = (Button) findViewById(R.id.yes);
        no = (Button) findViewById(R.id.no);
        titleTv = (TextView) findViewById(R.id.title);
        is_no_type_point=true;
        message_input_type="";

        message_input = (EditText) findViewById(R.id.editText);
        message_input.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //L.e("---------onTextChanged-----------");
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                //L.e("---------beforeTextChanged-----------");
            }

            @Override
            public void afterTextChanged(Editable s) {
                //L.e("---------afterTextChanged-----------");
            }
        });

        mContractBtn = (Button)findViewById(R.id.contract_btn);


    }

    /**
     * 从外界Activity为Dialog设置标题
     *
     * @param title
     */
    public void setTitle(String title) {
        titleStr = title;
    }

    public String getInput() {
        return message_input.getText().toString();
    }



    public String gettype() {
        return message_input_type;
    }



    /**
     * 从外界Activity为Dialog设置dialog的message
     *
     * @param message
     */
    public void setMessage(String message) {
        messageStr = message;
    }

    /**
     * 设置确定按钮和取消被点击的接口
     */
    public interface onYesOnclickListener {
        public void onYesClick();
    }

    public interface onNoOnclickListener {
        public void onNoClick();
    }
}  